<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: action.topic.php Sat Jul 18 12:46:00 CST 2009
 */

include "conf.inc";
$myts = MyTextSanitizer::getInstance();

$op = isset($_POST["op"]) ? $_POST["op"] : "";
$ac = isset($_POST["ac"]) ? $_POST["ac"] : "";
$group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0 ;
$tids = isset($_POST["tids"]) ? $_POST["tids"] : 0 ;

if ( !is_object($xoopsUser) ) {
	redirect_header(XOOPS_URL."/user.php",5,_NOPERM);
}

if ( false === getIsGroupsAdmin($group_id,$xoopsUser->getVar("uid")) ) {
	redirect_header("groups.php?op=forum&gid=".$group_id,5,_NOPERM);
}

$action_arr = array(
	"0"            => "",
	"displaytop"   => _GP_TOPIC_DISPLAYTOP,
	"highlight"    => _GP_TOPIC_HIGHLIGHT,
	"digest"       => _GP_TOPIC_DIGEST,
);

/** 加载相关的类文件 **/
$groups_handler = xoops_getmodulehandler("groups");
$threads_handler = xoops_getmodulehandler("threads");

$groups_obj = $groups_handler->get($group_id);
$groupinfo = $groups_obj->getValues();

/** 群组菜单导航 **/
$_links = $groups_handler->getLinkToGroupMenu($group_id);
$groupinfo = array_merge($groupinfo,$_links);
$groupinfo["group_logo"] = $groups_obj->groupLogo();
$groupinfo["groupstatus"]=$groups_obj->groupStatus();
$_uid = ( !empty($xoopsUser) && is_object($xoopsUser) ) ? $xoopsUser->getVar("uid") : 0;
$groupinfo["group_join"] = ( false === $groups_obj->isCreateUser($_uid) ) ? $groups_obj->getGroupJoinLink($_uid) : "";
$_op = "forum";
switch ($op) {
	case "displaytop":
		switch ($ac) {
			default:
				$xoopsOption["template_main"] = "groups_topic_type.html";
				include XOOPS_ROOT_PATH.'/header.php';
				if ( count($tids) ) {
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					$criteria->setOrder("DESC");
					$criteria->setSort("threads_lastdate");
					$topic_list = $threads_handler->getAll($criteria,array("threads_subject","threads_attachments","threads_displaytop","uid","author"),false);
					$xoopsTpl->assign(array("topic_list"=>$topic_list));
				}
				$form = new XoopsForm("", "form", "", "post",true);
				$form->addElement( new XoopsFormRadioYN(_GP_TOPIC_DISPLAYTOP_OPERATION,"displaytop",1,_GP_TOPIC_DISPLAYTOP,_GP_TOPIC_DISPLAYTOP_CANCEL));
				$form->addElement(new XoopsFormHidden("group_id",$group_id));
				$form->addElement(new XoopsFormHidden("op","displaytop"));
				$form->addElement(new XoopsFormHidden("ac","savedisplaytop"));
				$form->addElement(new XoopsFormButton("","submit",_SUBMIT,"submit"));
				$form->assign($xoopsTpl);
				$xoopsTpl->assign(array("groupinfo"=>$groupinfo,"op"=>$_op));
				$xoopsTpl->assign(array("action_desc"=>_GP_TOPIC_DISPLAYTOP_DESC,"redirect_url"=>"<a href=\"groups.php?op=forum&gid={$group_id}\">"._GP_BACK."</a>"));
			break;
			
			case "savedisplaytop":
				if (!$GLOBALS["xoopsSecurity"]->check()) {
		            redirect_header("groups.php?op=forum&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
		            exit();
		        }
				if ( count($tids) ) {
					$displaytop = isset($_POST["displaytop"]) ? intval($_POST["displaytop"]) : 0 ;
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					if ( $threads_handler->updateAll("threads_displaytop",$displaytop,$criteria) ) {
						$stop = _GP_SUCCEED;
					} else {
						$stop = _GP_FAILED;
					}
				} else {
					$stop = _GP_NO_TOPIC;
				}
				redirect_header("groups.php?op=forum&gid={$group_id}", 5, $stop);
			break;
		}
	break;
	
	case "highlight":
		switch ($ac) {
			default:
				$xoopsOption["template_main"] = "groups_topic_type.html";
				include XOOPS_ROOT_PATH.'/header.php';
				if ( count($tids) ) {
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					$criteria->setOrder("DESC");
					$criteria->setSort("threads_lastdate");
					$topic_list = $threads_handler->getAll($criteria,array("threads_subject","threads_attachments","threads_displaytop","uid","author"),false);
					$xoopsTpl->assign(array("topic_list"=>$topic_list));
				}
				$form = new XoopsForm("", "form", "action.topic.php", "post",true);
				$form->addElement( new XoopsFormRadioYN(_GP_TOPIC_HIGHLIGHT_OPERATION,"highlight",1,_GP_TOPIC_HIGHLIGHT,_GP_TOPIC_HIGHLIGHT_CANCEL));
				
				$font_type = new XoopsFormCheckBox(_GP_TOPIC_FONT_TYPE,"font_type","4","&nbsp;");
				$font_type->addOption("4",_GP_TOPIC_FONTWEIGHT_B);
				$font_type->addOption("2",_GP_TOPIC_FONTWEIGHT_I);
				$font_type->addOption("1",_GP_TOPIC_FONTWEIGHT_U);
				$form->addElement($font_type);
				
				$form->addElement( new XoopsFormColorPicker(_GP_TOPIC_FONT_COLOR,"font_color","#006699"));
				$form->addElement(new XoopsFormHidden("group_id",$group_id));
				$form->addElement(new XoopsFormHidden("op","highlight"));
				$form->addElement(new XoopsFormHidden("ac","savehighlight"));
				$form->addElement(new XoopsFormButton("","submit",_SUBMIT,"submit"));
				$form->assign($xoopsTpl);
				$xoopsTpl->assign(array("groupinfo"=>$groupinfo,"op"=>$_op));
				$xoopsTpl->assign(array("action_desc"=>_GP_TOPIC_HIGHLIGHT_DESC,"redirect_url"=>"<a href=\"groups.php?op=forum&gid={$group_id}\">"._GP_BACK."</a>"));
			break;
			
			case "savehighlight":
				if (!$GLOBALS["xoopsSecurity"]->check()) {
		            redirect_header("groups.php?op=forum&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
		            exit();
		        }
				if ( count($tids) ) {
					$highlight = isset($_POST["highlight"]) ? intval($_POST["highlight"]) : 0 ;
					$font_type = isset($_POST["font_type"]) ? $_POST["font_type"] : "" ;
					$font_color = isset($_POST["font_color"]) ? trim($_POST["font_color"]) : "" ;
					
					$highlight_val = 0 ;
					if ( count($font_type) && !empty($highlight) ) {
						foreach ( $font_type as $val ) {
							$highlight_val = $highlight_val + $val;
						}
					}
					
					if ( !empty($highlight) ) {
						if ( preg_match("/^#[0-9a-fA-F]{6}$/", $font_color)){
					        $highlight_val .= $font_color; 
					    } 
					}
					
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					if ( $threads_handler->updateAll("threads_highlight",$highlight_val,$criteria) ) {
						$stop = _GP_SUCCEED;
					} else {
						$stop = _GP_FAILED;
					}
				} else {
					$stop = _GP_NO_TOPIC;
				}
				redirect_header("groups.php?op=forum&gid={$group_id}", 5, $stop);
			break;
		}
	break;
	
	case "digest":
		switch ($ac) {
			default:
				$xoopsOption["template_main"] = "groups_topic_type.html";
				include XOOPS_ROOT_PATH.'/header.php';
				if ( count($tids) ) {
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					$criteria->setOrder("DESC");
					$criteria->setSort("threads_lastdate");
					$topic_list = $threads_handler->getAll($criteria,array("threads_subject","threads_attachments","threads_displaytop","uid","author"),false);
					$xoopsTpl->assign(array("topic_list"=>$topic_list));
				}
				$form = new XoopsForm("", "form", "action.topic.php", "post",true);
				$form->addElement( new XoopsFormRadioYN(_GP_TOPIC_DIGEST_OPERATION,"digest",1,_GP_TOPIC_DIGEST,_GP_TOPIC_DIGEST_CANCEL));
				$form->addElement(new XoopsFormHidden("group_id",$group_id));
				$form->addElement(new XoopsFormHidden("op","digest"));
				$form->addElement(new XoopsFormHidden("ac","savedigest"));
				$form->addElement(new XoopsFormButton("","submit",_SUBMIT,"submit"));
				$form->assign($xoopsTpl);
				$xoopsTpl->assign(array("groupinfo"=>$groupinfo,"op"=>$_op));
				$xoopsTpl->assign(array("action_desc"=>_GP_TOPIC_DIGEST_DESC,"redirect_url"=>"<a href=\"groups.php?op=forum&gid={$group_id}\">"._GP_BACK."</a>"));
			break;
			case "savedigest":
				if (!$GLOBALS["xoopsSecurity"]->check()) {
		            redirect_header("groups.php?op=forum&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
		            exit();
		        }
				if ( count($tids) ) {
					$digest = isset($_POST["digest"]) ? intval($_POST["digest"]) : 0 ;
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					if ( $threads_handler->updateAll("threads_digest",$digest,$criteria) ) {
						$stop = _GP_SUCCEED;
					} else {
						$stop = _GP_FAILED;
					}
				} else {
					$stop = _GP_NO_TOPIC;
				}
				redirect_header("groups.php?op=forum&gid={$group_id}", 5, $stop);
			break;
		}
	break;
	
	case "deletetopics":
		switch ($ac) {
			default:
				$xoopsOption["template_main"] = "groups_topic_type.html";
				include XOOPS_ROOT_PATH.'/header.php';
				if ( count($tids) ) {
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					$criteria->setOrder("DESC");
					$criteria->setSort("threads_lastdate");
					$topic_list = $threads_handler->getAll($criteria,array("threads_subject","threads_attachments","threads_displaytop","uid","author"),false);
					$xoopsTpl->assign(array("topic_list"=>$topic_list));
				}
				$form = new XoopsForm("", "form", "action.topic.php", "post",true);
				$form->addElement(new XoopsFormHidden("group_id",$group_id));
				$form->addElement(new XoopsFormHidden("op","deletetopics"));
				$form->addElement(new XoopsFormHidden("ac","savedeletetopics"));
				$form->addElement(new XoopsFormButton("","submit",_SUBMIT,"submit"));
				$form->assign($xoopsTpl);
				$xoopsTpl->assign(array("groupinfo"=>$groupinfo,"op"=>$_op));
				$xoopsTpl->assign(array("action_desc"=>_GP_TOPIC_DELETE,"redirect_url"=>"<a href=\"groups.php?op=forum&gid={$group_id}\">"._GP_BACK."</a>"));
			break;
			
			case "savedeletetopics":
				if (!$GLOBALS["xoopsSecurity"]->check()) {
		            redirect_header("groups.php?op=forum&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
		            exit();
		        }
				if ( count($tids) ) {
				    $posts_handler = xoops_getmodulehandler("posts","groups");
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					if ( $threads_handler->deleteAll($criteria) ) {
					    $_post_ids = $posts_handler->getIds($criteria);
                        if ( $_post_ids ) {
                            foreach ( $_post_ids as $k=>$_post_id ) {
                                $posts_handler->delPost($_post_id);
                            }
                        }
					    $num = count($tids) ;
					    $_num = count($_post_ids);
					    $groups_handler->changeTopics($group_id,-1*$num,-1*$_num);
						$stop = _GP_SUCCEED;
					} else {
						$stop = _GP_FAILED;
					}
				} else {
					$stop = _GP_NO_TOPIC;
				}
				redirect_header("groups.php?op=forum&gid={$group_id}", 5, $stop);
			break;
		}
	break;
	
	default:
	    redirect_header("groups.php?op=forum&gid={$group_id}", 5, _GP_ERROR_OPERATION);
	break;
}

include "footer.php";
?>